//
//  ViewController.m
//  TEST_CoreImage
//
//  Created by lianglibao on 2019/4/12.
//  Copyright © 2019 lianglibao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)detectorFaceImage:(CIImage *)myImage {
    CIContext * context = [CIContext context]; // 1
    NSDictionary * opts = @ {CIDetectorAccuracy:CIDetectorAccuracyHigh}; // 2
    CIDetector * detector = [CIDetector detectorOfType:CIDetectorTypeFace context:context options:opts]; // 3
    opts = @ {CIDetectorImageOrientation:[[myImage properties]
                                          valueForKey:(__bridge NSString*) kCGImagePropertyOrientation]}; // 4
    NSArray * features = [detector featuresInImage:myImage options:opts]; // 5
    
    for (CIFaceFeature *f in features) {
        NSLog(@"%@", NSStringFromCGRect(f.bounds));
        
        if (f.hasLeftEyePosition) {
            NSLog(@"Left eye %g %g", f.leftEyePosition.x, f.leftEyePosition.y);
        }
        if (f.hasRightEyePosition) {
            NSLog(@"Right eye %g %g", f.rightEyePosition.x, f.rightEyePosition.y);
        }
        if (f.hasMouthPosition) {
            NSLog(@"Mouth %g %g", f.mouthPosition.x, f.mouthPosition.y);
        }
    }
    
    NSDictionary *options = @{ CIDetectorImageOrientation :
                                   [[myImage properties] valueForKey:(__bridge NSString*)kCGImagePropertyOrientation] };
    NSArray *adjustments = [myImage autoAdjustmentFiltersWithOptions:options];
    for (CIFilter *filter in adjustments) {
        [filter setValue:myImage forKey:kCIInputImageKey];
        myImage = filter.outputImage;
    }
}

- (void)querySystemFilters {
    NSMutableDictionary *filtersByCategory = [NSMutableDictionary dictionary];
    
    NSMutableArray *filterNames = [NSMutableArray array];
    [filterNames addObjectsFromArray:
     [CIFilter filterNamesInCategory:kCICategoryGeometryAdjustment]]; // 几何调整，如仿射变换，裁剪，透视变换
    [filterNames addObjectsFromArray:
     [CIFilter filterNamesInCategory:kCICategoryDistortionEffect]];   // 半色调效果，如屏幕，线条屏幕，阴影线
    filtersByCategory[@"Distortion"] = [self buildFilterDictionary: filterNames];
    
    [filterNames removeAllObjects];
    [filterNames addObjectsFromArray:
     [CIFilter filterNamesInCategory:kCICategorySharpen]];   // 锐化，亮度
    [filterNames addObjectsFromArray:
     [CIFilter filterNamesInCategory:kCICategoryBlur]];      // 模糊，如高斯，变焦，运动
    filtersByCategory[@"Focus"] = [self buildFilterDictionary: filterNames];
}

- (NSMutableDictionary *)buildFilterDictionary:(NSArray *)filterClassNames {
    NSMutableDictionary *filters = [NSMutableDictionary dictionary];        // 1
    for (NSString *className in filterClassNames) {                         // 2
        CIFilter *filter = [CIFilter filterWithName:className];             // 3
        
        if (filter) {
            filters[className] = [filter attributes];                       // 4
        } else {
            NSLog(@"could not create '%@' filter", className);
        }
    }
    return filters;
}

/*
 效果类型                                       指示
 
 kCICategoryDistortionEffect              失真效果，如凹凸，旋转，洞
 
 kCICategoryGeometryAdjustment            几何调整，如仿射变换，裁剪，透视变换
 
 kCICategoryCompositeOperation            合成，例如source over，minimum，source atop，color dodge blend mode
 
 kCICategoryHalftoneEffect                半色调效果，如屏幕，线条屏幕，阴影线
 
 kCICategoryColorAdjustment               颜色调整，如伽马调整，白点调整，曝光
 
 kCICategoryColorEffect                   色彩效果，如色调调整，张贴
 
 kCICategoryTransition                    图像之间的过渡，例如溶解，与面具分解，滑动
 
 kCICategoryTileEffect                    平铺效果，如平行四边形，三角形
 
 kCICategoryGenerator                     图像生成器，如条纹，恒定颜色，棋盘格
 
 kCICategoryGradient                      梯度，如轴向，径向，高斯
 
 kCICategoryStylize                       风格化，例如像素，结晶
 
 kCICategorySharpen                       锐化，亮度
 
 kCICategoryBlur                          模糊，如高斯，变焦，运动
 
 表4-2   过滤器使用的过滤器类别常量
 使用                                             指示
 
 kCICategoryStillImage                    可用于静止图像
 
 kCICategoryVideo                         可用于视频
 
 kCICategoryInterlaced                    可用于隔行扫描图像
 
 kCICategoryNonSquarePixels               可用于非方形像素
 
 kCICategoryHighDynamicRange              可用于高动态范围像素
 

 */

@end
